package com.zhou.JavaAndroidFirm.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * @author Administrator
 * @date 2018/7/25 0025
 * @des 实现图像根据按下抬起动作变化颜色
 */
class ColorFilterImageView extends ImageView implements View.OnTouchListener {
    public ColorFilterImageView(Context context) {
        this(context, null, 0);
    }

    public ColorFilterImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorFilterImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    public void init() {
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);//按下背景色变灰
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                //抬起或者取消时候恢复透明色
                setColorFilter(Color.TRANSPARENT);
            default:
                break;
        }
        return false;
    }
}
